package com.beijiteshop.shop.ui.order.fragment

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.beijiteshop.libcommon.base.adapter.showEmpty
import com.beijiteshop.libcommon.base.adapter.showError
import com.beijiteshop.libcommon.base.adapter.showLoading
import com.beijiteshop.libcommon.base.adapter.showNetError
import com.beijiteshop.libcommon.ext.databinding.bindView
import com.beijiteshop.shop.R
import com.beijiteshop.shop.app.PageIndex
import com.beijiteshop.shop.base.BaseContainerFragment
import com.beijiteshop.shop.databinding.FragmentOrderListBinding
import com.beijiteshop.shop.model.api.response.OrderListBean
import com.beijiteshop.shop.ui.order.adapter.OrderListAdapter
import com.beijiteshop.shop.ui.order.vm.OrderVM
import org.koin.android.viewmodel.ext.android.viewModel

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/11 11:15
 * @Description 订单列表
 */
class OrderListFragment : BaseContainerFragment(R.layout.fragment_order_list) {

    companion object{

        const val BUNDLE_ORDER_ID = "bundle_order_id"
        private const val INIT_PAGE = 1

        /**
         * @param id 订单id
         */
        fun newInstance(id: Int): OrderListFragment{
            return OrderListFragment().apply {
                arguments = Bundle().apply {
                    putInt(BUNDLE_ORDER_ID,id)
                }
            }
        }
    }

    private val mBinding by bindView<FragmentOrderListBinding>()
    private var mOrderDetailAdapter = OrderListAdapter(mutableListOf())
    private val mOrderVM: OrderVM by viewModel()
    private var mIndex: Int? = null
    private var mPage: Int = INIT_PAGE


    override fun onLazyLoad() {
        initOrderList()
    }

    override fun initView(view: View) {
        mIndex = arguments?.getInt(BUNDLE_ORDER_ID)
        initRV()
    }

    override fun initData() {
    }

    override fun initEvent() {

        mOrderVM.apply {
            liveOrderList.observeKt {list ->
                list?.let {
                    if (mPage == INIT_PAGE){
                        mOrderDetailAdapter.setNewInstance(it.toMutableList())
                    }else{
                        updateRVState(false,it.toMutableList() )
                    }
                }
            }

            liveNetError.observeKt {
                if (mPage == INIT_PAGE){
                    mOrderDetailAdapter.showNetError(it){
                        initOrderList()
                    }
                }else{
                    updateRVState(true,null)
                }
            }

            liveError.observeKt {
                if (mPage == INIT_PAGE){
                    mOrderDetailAdapter.showError(it){
                        initOrderList()
                    }
                }else{
                    updateRVState(true,null)
                }
            }

            liveEmpty.observeKt {
                if (mPage == INIT_PAGE){
                    mOrderDetailAdapter.showEmpty { initOrderList() }
                }else{
                    updateRVState(false,null)
                }
            }
        }
    }

    private fun initRV(){

        mOrderDetailAdapter.apply {
            loadMoreModule.setOnLoadMoreListener {
                mPage++
                requestData()
            }

            setOnItemClickListener { _, view, position ->
                val bundle = Bundle().apply {
                    putString(OrderDetailFragment.BUNDLE_ORDER_ID,data[position].bus_OrdersID)
                }
                startActivity(PageIndex.ORDER_DETAIL,bundle)
            }

            setOnItemChildClickListener { adapter, view, position ->
                when(view.id){
                    R.id.order_list_pay_btn -> {
                        Log.d("TAG","ordernum:${data[position].orderNumber}")
                        startActivity(PageIndex.ORDER_PAY,Bundle().apply {
                            putString(OrderPayFragment.BUNDLE_ORDER_NUMBER,data[position].orderNumber)
                        })
                    }
                }
            }
        }

        mBinding.orderDetailRv.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = mOrderDetailAdapter
        }
    }

    private fun initOrderList(){
        mOrderDetailAdapter.showLoading()
        requestData()
    }

    private fun requestData(){
        //类型为全部时，index传null
        if (mIndex == null || mIndex == 0){
            mIndex = null
            mOrderVM.getOderList(mPage,mIndex)
        }else{
            mOrderVM.getOderList(mPage,mIndex!!-1)
        }

    }

    /**
     * 更新列表加载状态
     */
    private fun updateRVState(error: Boolean,data: MutableList<OrderListBean>?){
        if (error){
            if (mOrderDetailAdapter.loadMoreModule.isLoading){
                mOrderDetailAdapter.loadMoreModule.loadMoreFail()
            }
        }else{
            if (mOrderDetailAdapter.loadMoreModule.isLoading){
                if (data.isNullOrEmpty()){
                    mOrderDetailAdapter.loadMoreModule.loadMoreEnd()
                }else{
                    mOrderDetailAdapter.addData(data)
                    mOrderDetailAdapter.loadMoreModule.loadMoreComplete()
                }
            }
        }
    }
}